//
//  HBAudioPlayControlView.m
//  HappyBaby
//
//  Created by lanou on 15/11/28.
//  Copyright © 2015年 华哥. All rights reserved.
//

#import "HBAudioPlayControlView.h"
#import <Masonry.h>

@interface HBAudioPlayControlView ()

/** 进度时间 */
@property (nonatomic, weak) UILabel *playTimeView;

/** 总时间 */
@property (nonatomic, weak) UILabel *durationView;

/** 进度条 */
@property (nonatomic, weak) UISlider *slider;

/** 中间button */
@property (nonatomic, weak) UIButton *middleBtn;

@end

@implementation HBAudioPlayControlView
{
    AudioPlayIndex _audioPlayIndex;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.translatesAutoresizingMaskIntoConstraints = NO;
        //1.创建控制按钮button
        [self makeView];
        
        // 2.创建进度条
        [self makeProgress];
    }
    return self;
}

#pragma mark 创建进度条
- (void)makeProgress
{
    // 进度时间
    UILabel *playTime = [UILabel new];
    playTime.font = [UIFont systemFontOfSize:12];
    playTime.textColor = [UIColor whiteColor];
    playTime.text = @"00:00";
    [self addSubview:playTime];
    self.playTimeView = playTime;
    
    // 进度条
    UISlider *slider = [UISlider new];
    [self addSubview:slider];
    self.slider = slider;
    
    // 总时间
    UILabel *duration = [UILabel new];
    duration.font = [UIFont systemFontOfSize:12];
    duration.textColor = [UIColor whiteColor];
    duration.text = @"12:34";
    [self addSubview:duration];
    self.durationView = duration;
    
    // 添加约束
    __weak typeof(self) weakSelf = self;
    [slider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf);
        make.top.equalTo(weakSelf);
        make.width.equalTo(weakSelf).multipliedBy(0.7);
    }];
    
    [playTime mas_makeConstraints:^(MASConstraintMaker *make) {
//        make..equalTo(slider.mas_left).with.offset(-5);
        make.right.mas_equalTo(slider.mas_left).with.offset(-5);
        make.centerY.equalTo(slider);
    }];
    
    [duration mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(slider.mas_right).with.offset(5);
        make.centerY.equalTo(slider);
    }];
}

#pragma mark 创建View
- (void)makeView
{
    // 中间button
    UIButton *middleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [middleBtn setImage:[UIImage imageNamed:@"pauseAudio"] forState:UIControlStateNormal];
    [middleBtn setImage:[UIImage imageNamed:@"bofang"] forState:UIControlStateSelected];
    [self addSubview:middleBtn];
    self.middleBtn = middleBtn;

    [middleBtn addTarget:self action:@selector(middleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    // 左边
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setImage:[UIImage imageNamed:@"previousAudio"] forState:UIControlStateNormal];
    [self addSubview:leftBtn];
     [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    // 右边
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setImage:[UIImage imageNamed:@"nextAudio"] forState:UIControlStateNormal];
    [self addSubview:rightBtn];

    [rightBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    // 添加约束
    __weak typeof(self) weakSelf = self;
    [middleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf);
        make.width.and.height.equalTo(weakSelf.mas_width).multipliedBy(0.1);
        make.bottom.equalTo(weakSelf);
    }];
    
    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(middleBtn);
        make.right.equalTo(middleBtn.mas_left).with.offset(-25);
        make.width.and.height.equalTo(middleBtn.mas_width).multipliedBy(0.9);
    }];
    
    [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(middleBtn);
        make.left.equalTo(middleBtn.mas_right).with.offset(25);
        make.width.and.height.equalTo(middleBtn.mas_width).multipliedBy(0.9);
    }];
}


#pragma mark 点击中间按钮
- (void)middleBtnClick:(UIButton *)btn
{
    btn.selected = !btn.selected;
    
    _audioPlayIndex = MiddleBtnIndex;
    
    if ([self.delegate respondsToSelector:@selector(audioPlayBtnClick:buttonIndex:)])
    {
        [self.delegate audioPlayBtnClick:self buttonIndex:_audioPlayIndex];
    }
    
}

#pragma mark 点击左边按钮
- (void)leftBtnClick:(UIButton *)btn
{
    _audioPlayIndex = LeftBtnIndex;
    if ([self.delegate respondsToSelector:@selector(audioPlayBtnClick:buttonIndex:)])
    {
        [self.delegate audioPlayBtnClick:self buttonIndex:_audioPlayIndex];
    }
}

#pragma mark 点击右边按钮
- (void)rightBtnClick:(UIButton *)btn
{
    _audioPlayIndex = RightBtnIndex;
    if ([self.delegate respondsToSelector:@selector(audioPlayBtnClick:buttonIndex:)])
    {
        [self.delegate audioPlayBtnClick:self buttonIndex:_audioPlayIndex];
    }
}

#pragma mark set方法设置
- (void)setDuration:(NSTimeInterval)duration
{
    NSInteger time = (NSInteger)duration;
    // 分钟
    NSInteger min = time / 60;
    
    // 秒数
    NSInteger sec = time % 60;
    
    self.durationView.text = [NSString stringWithFormat:@"%02ld:%02ld", min, sec];
    // 设置进度条范围
    self.slider.minimumValue = 0;
    self.slider.maximumValue = duration;
}

- (void)setPlayingTime:(NSTimeInterval)playingTime
{
    _playingTime = playingTime;
    
    NSInteger time = (NSInteger)playingTime;
    // 分钟
    NSInteger min = time / 60;
    // 秒数
    NSInteger sec = time % 60;
    
    self.playTimeView.text = [NSString stringWithFormat:@"%02ld:%02ld", min, sec];
    self.slider.value = playingTime;
}

- (void)setIsPlaying:(BOOL)isPlaying
{
    _isPlaying = isPlaying;
    self.middleBtn.selected = isPlaying;
}

@end
